<template>
  <div class="app-container">
    <el-card class="mb20">
      <el-form :model="listQF" inline>
        <el-form-item label="单据号">
          <el-input
            placeholder="单据号"
            v-model="listQF.carryApplyNo"
          ></el-input>
        </el-form-item>
        <el-form-item label="出库单号">
          <el-input
            placeholder="出库单号"
            v-model="listQF.outStockNo"
          ></el-input>
        </el-form-item>
        <el-form-item label="发货单号">
          <el-input
            placeholder="发货单号"
            v-model="listQF.deliveryNo"
          ></el-input>
        </el-form-item>
        <el-form-item label="客户">
          <el-select v-model="listQF.customerId" placeholder="客户">
            <el-option
              v-for="item in customerList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请人">
          <el-select v-model="listQF.applyUser" placeholder="申请人">
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.realName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间">
          <el-date-picker
            v-model="listQF.applyAttr"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请状态">
          <el-select v-model="listQF.applyStatus" placeholder="申请状态">
            <el-option label="未提交" value="未提交"></el-option>
            <el-option label="待审核" value="待审核"></el-option>
            <el-option label="已审核" value="已审核"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核人">
          <el-select v-model="listQF.approveUser" placeholder="审核人">
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.realName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核时间">
          <el-date-picker
            v-model="listQF.approveAttr"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态">
          <el-select v-model="listQF.approveStatus" placeholder="审核状态">
            <el-option label="通过" value="通过"></el-option>
            <el-option label="驳回" value="驳回"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table :data="list">
        <el-table-column
          v-for="item in listC"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200px"
          v-slot="{ row }"
        >
          <el-button type="primary" size="mini" @click="handleEdit(row)"
            >修改</el-button
          >
          <el-button type="danger" size="mini">删除</el-button>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.page"
        :page-sizes="[2, 10, 20, 30]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      center
      title="修改承运申请"
      :visible.sync="visible"
      @close="close"
    >
      <el-form ref="listEFRef" :model="listEF" inline>
        <el-form-item label="单据号" prop="carryApplyNo">
          <el-input
            v-model="listEF.carryApplyNo"
            placeholder="单据号"
          ></el-input>
        </el-form-item>
        <el-form-item label="出库单号" prop="outStockNo">
          <el-input
            v-model="listEF.outStockNo"
            placeholder="出库单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="发货单号" prop="deliveryNo">
          <el-input
            v-model="listEF.deliveryNo"
            placeholder="发货单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="客户" prop="customer.id" required>
          <el-select v-model="listEF.customer.id" placeholder="客户">
            <el-option
              v-for="item in customerList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交货日期" prop="deliveryDate">
          <el-date-picker
            v-model="listEF.deliveryDate"
            type="date"
            placeholder="交货日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收货地址" prop="deliveryAddress" required>
          <el-input
            v-model="listEF.deliveryAddress"
            placeholder="收货地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contact" required>
          <el-input v-model="listEF.contact" placeholder="联系人"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone" required>
          <el-input v-model="listEF.phone" placeholder="联系电话"></el-input>
        </el-form-item>
        <el-form-item label="承运人" prop="carrier" required>
          <el-input v-model="listEF.carrier" placeholder="承运人"></el-input>
        </el-form-item>
        <el-form-item label="运输距离" prop="distance" required>
          <el-input v-model="listEF.distance" placeholder="运输距离"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            v-model="listEF.remark"
            placeholder="备注"
          ></el-input>
        </el-form-item>
        <el-form-item label="申请人" prop="applyUser">
          <el-input
            v-if="listEF.applyUser"
            v-model="listEF.applyUser.realName"
            placeholder="申请人"
          ></el-input>
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker
            v-model="listEF.applyTime"
            type="date"
            placeholder="申请时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请状态" prop="applyStatus">
          <el-select v-model="listEF.applyStatus" placeholder="申请状态">
            <el-option label="未提交" value="未提交"></el-option>
            <el-option label="待审核" value="待审核"></el-option>
            <el-option label="已审核" value="已审核"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核人" prop="approveUser">
          <el-input
            v-model="listEF.approveUser.realName"
            placeholder="审核人"
          ></el-input>
        </el-form-item>
        <el-form-item label="审核时间" prop="approveTime">
          <el-date-picker
            v-model="listEF.approveTime"
            type="date"
            placeholder="审核时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态" prop="approveStatus">
          <el-select v-model="listEF.approveStatus" placeholder="审核状态">
            <el-option label="通过" value="通过"></el-option>
            <el-option label="驳回" value="驳回"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-table :data="listEF.detailList">
        <el-table-column label="序号" v-slot="{ $index }">{{
          $index
        }}</el-table-column>
        <el-table-column
          v-for="item in detailListC"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
      </el-table>
      <template #footer>
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCarrierApplication, editCarrierApplication } from '@/api_zc/wms/transportation-management/carrier-application'
import { getApplyApproveUserList, getCustomerList } from '@/api_zc/task/sale-order-approve'
export default {
  mounted () {
    this.getList()
    this.getUnitylist()
  },
  data () {
    return {
      list: [],
      pager: {
        page: 1,
        limit: 10,
        total: 0
      },
      listQF: {
        carryApplyNo: '',
        outStockNo: '',
        deliveryNo: '',
        customerId: '',
        applyUser: '',
        applyAttr: [],
        applyStartTime: '',
        applyEndTime: '',
        applyStatus: '',
        approveUser: '',
        approveAttr: [],
        approveStartTime: '',
        approveEndTime: '',
        approveStatus: ''
      },
      listEF: {
        detailList: [],
        customer: {},
        applyUser: {},
        approveUser: {}
      },
      listC: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '单据号',
          prop: 'carryApplyNo'
        },
        {
          label: '出库单号',
          prop: 'outStockNo'
        },
        {
          label: '客户',
          prop: 'customer.name'
        },
        {
          label: '收货地址',
          prop: 'deliveryAddress'
        },
        {
          label: '联系人',
          prop: 'contact'
        },
        {
          label: '联系电话',
          prop: 'phone'
        },
        {
          label: '承运人',
          prop: 'carrier'
        },
        {
          label: '交货日期',
          prop: 'deliveryDate'
        },
        {
          label: '运输距离',
          prop: 'distance'
        },
        {
          label: '申请人',
          prop: 'applyUser.realName'
        },
        {
          label: '申请日期',
          prop: 'applyTime'
        },
        {
          label: '申请状态',
          prop: 'applyStatus'
        },
        {
          label: '审核人',
          prop: 'approveUser.realName'
        },
        {
          label: '审核日期',
          prop: 'approveTime'
        },
        {
          label: '审核状态',
          prop: 'approveStatus'
        }
      ],
      visible: false,
      customerList: [],
      userList: [],
      detailListC: [
        {
          label: '物料',
          prop: 'material.name'
        },
        {
          label: '生产批号',
          prop: 'batchNo'
        },
        {
          label: '仓库',
          prop: 'warehouse.name'
        },
        {
          label: '库区',
          prop: 'area.code'
        },
        {
          label: '库位',
          prop: 'location.code'
        },
        {
          label: '发货数量',
          prop: 'quantity'
        }
      ]
    }
  },
  methods: {
    async getList () {
      [this.listQF.applyStartTime, this.listQF.applyEndTime] = this.listQF.applyAttr;
      [this.listQF.approveStartTime, this.listQF.approveEndTime] = this.listQF.approveAttr
      const { data: { content, total } } = await getCarrierApplication({ ...this.listQF, page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    close () {
      this.visible = false
    },
    handleReset () {
      Object.assign(this.listQF, this.$options.data().listQF)
      this.getList()
    },
    async getUnitylist () {
      const data1 = await getCustomerList()
      this.customerList = data1.data.content
      const data2 = await getApplyApproveUserList()
      this.userList = data2.data
    },
    handleSizeChange (val) {
      this.pager.limit = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.pager.page = val
      this.getList()
    },
    handleEdit (row) {
      this.visible = true
      this.listEF = {
        ...row,
        approveUser: row.approveUser ?? { realName: null }
      }
      // Object.assign(this.listEF, {
      //   ...row,
      //   approveUser: row.approveUser ?? { realName: null }
      // })
    },
    async submit () {
      this.listEF.applyTime = new Date(this.listEF.applyTime)
      this.listEF.approveTime = new Date(this.listEF.approveTime)
      this.listEF.deliveryDate = new Date(this.listEF.deliveryDate)
      await editCarrierApplication(this.listEF)
      this.$message.success('修改成功')
      this.visible = false
      this.getList()
    }
  }
}
</script>

<style>
</style>